
/*
 *
 * Copyright (C) 2008 Mattias Blomqvist, patr-blo at dsv dot su dot se
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using FlowLib.Events;
using ClientExample.Containers;

namespace ClientExample.Controls
{
    public partial class ExpandablePanel : UserControl
    {
        public event EventHandler<FmdcEventArgs<int, object>> ExpandedChanged;
        protected delegate void Change();

        protected int intExpandedHeight = 0;
        protected bool bExpanded = true;

        public bool Expanded
        {
            get
            {
                return bExpanded;
            }
            set
            {
                bExpanded = value;
				while(Handle == null) { }
                Invoke(new Change(OnChangeSize));
            }
        }

        protected void OnChangeSize()
        {
            int h = this.Height;
            ChangeSize();
            ExpandedChanged(this, new FmdcEventArgs<int, object>(h, null));
        }

        public Label TopLabel
        {
            get { return label1; }
        }

        public Panel Panel
        {
            get { return splitContainer1.Panel2; }
        }

        public ExpandablePanel()
        {
			IntPtr h = Handle;  // This is for stupid mono stuff. Some how handle isnt created correctly if we dont do this :(
            InitializeComponent();
            ExpandedChanged = new EventHandler<FmdcEventArgs<int, object>>(ExtendablePanel_ExpandedChanged);
            intExpandedHeight = this.Height;
            Expanded = true;

            splitContainer1.Panel1.MouseClick += new MouseEventHandler(Panel1_MouseClick);
            label1.MouseClick += new MouseEventHandler(Panel1_MouseClick);

            //splitContainer1.Panel1.Click += new EventHandler(Panel1_Click);
            //label1.Click += new EventHandler(Panel1_Click);
        }

        void Panel1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                Expanded = !Expanded;
        }

        void ExtendablePanel_ExpandedChanged(object sender, FmdcEventArgs<int, object> e) { }

        //void Panel1_Click(object sender, EventArgs e)
        //{
        //    Expanded = !Expanded;
        //}

        private void ExtendablePanel_Load(object sender, EventArgs e) { }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Expanded = !Expanded;
        }

        private void ChangeSize()
        {
            if (Expanded)
            {
                splitContainer1.Panel2Collapsed = false;
                Height = intExpandedHeight;
            }
            else
            {
                intExpandedHeight = this.Height;
                Height = splitContainer1.Panel1.Height;
                splitContainer1.Panel2Collapsed = true;
            }
            ChangeImage();
        }

        protected virtual void ChangeImage()
        {
            if (Expanded)
                pictureBox1.Image = Program.Images.GetBitmap("expand.gif");
            else
                pictureBox1.Image = Program.Images.GetBitmap("collapse.gif");
        }
    }
}
